home *** CD-ROM | disk | FTP | other *** search
- Path: linux.nildram.co.uk!news
- From: colin@greench.co.uk (Colin Wray)
- Newsgroups: comp.lang.c
- Subject: Re: What is byte-alignment?
- Date: Wed, 10 Apr 1996 21:58:01 GMT
- Organization: Greenchurch Software Ltd
- Message-ID: <4kheda$im5@linux.nildram.co.uk>
- References: <4jprfe$c6q@alcor.usc.edu>
- NNTP-Posting-Host: ppp4.nildram.co.uk
- X-Newsreader: Forte Free Agent 1.0.82
-
- wawda@alcor.usc.edu (Abu Wawda) wrote:
-
- >I often hear the phrase "byte-aligment" in this newsgroups, yet have
- >no clue what it is. Can someone please explain it to me? It comes up
- >often when people talk about structures. Thanks in advance,
-
- Well I guess you know all about it now.
-
- For anyone interested, you can make your code portable with the
- following macro:
-
- #define ALIGN_BOUND( type) \
- ( sizeof ( struct { char c; type t; } ) - sizeof ( type ) )
-
- Then a=ALIGN_BOUND(long); will give you the byte multiple on which
- the current compiler places a variable of type long. eg:
-
- a==4 if longs must start on an address which is a multiple of 4.
- a==2 if longs must start on an address which is a multiple of 2.
-
- etc. Works for any type you care to name of course. I had to use it in
- a library function for a database, where the caller passed the start
- address of his structure, and the variable types, but I had to guess
- the offsets for each variable.
-
- -Colin
-
- ----------------------------------------------------------------------------
- Colin Wray, Greenchurch Software Ltd, UK
- Email: colin@greench.co.uk
- ----------------------------------------------------------------------------
-
-